package networkmapping;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

import org.snmp4j.smi.Address;
import org.snmp4j.smi.IpAddress;

import com.sun.java.swing.plaf.windows.WindowsBorders.ProgressBarBorder;

import view.FrameWork;

public class NetworkMappingDialog extends JDialog {

	private static final long serialVersionUID = -5942264723544830185L;
	private JTextField textField;
	private JButton okButton;
	private FrameWork framwork;
	private NetworkMappingDialog dialog = this;

	public NetworkMappingDialog(FrameWork parent) {
		super(parent, "Start Point", true);
		this.framwork = parent;
		init();

	}

	private void init() {
//		this.setIconImage(Toolkit.getDefaultToolkit().getImage(
//				"images" + File.separator + "Discovery.png"));
		textField = new JTextField(30);
		textField.setText("127.0.0.1");

		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Validare l'indirizzo IP Immesso

				String address = textField.getText();
				IpAddress ipAddress = new IpAddress(address);
				if (ipAddress != null) {
					dialog.setVisible(false);

					
					//ProgressBar
					JProgressBar progressBar = new JProgressBar();
					progressBar.setIndeterminate(true);
					progressBar.setVisible(true);
					framwork.add(progressBar);
					//ProgressBar
					
					
					NetworkMappingManager mappingManager = new NetworkMappingManager();
					Host firstHost = mappingManager.getLanMap(ipAddress);
					progressBar.setVisible(false);

					// Visualizzare i risultati nel pannello
					MappingResultPanel mappingResultPanel = new MappingResultPanel(
							firstHost);
					framwork.setDesktopPanel(mappingResultPanel);
				} else {
					// Visualizzare messaggio di errore
					JOptionPane.showMessageDialog(null,
							"Inserire un indirizzo IP valio");
				}
			}

		});

		JPanel addressPane = new JPanel();
		addressPane
				.setBorder(BorderFactory
						.createTitledBorder("Inserire l'indirizzo del device da cui si vuole partire per il discovery"));
		addressPane.setAlignmentX(Component.LEFT_ALIGNMENT);

		addressPane.add(new JLabel("Indirizzo IP"));
		addressPane.add(textField);
		addressPane.add(okButton);

		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
		getContentPane().add(Box.createRigidArea(new Dimension(10, 10)));
		getContentPane().add(addressPane);
		pack();

		setLocation(framwork.getLocation().x + 200,
				framwork.getLocation().y + 200);
	}
}
